﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using WindowsLive.Writer.Api;

namespace SnippetPlugin {
    [WriterPlugin("6A81F382-A92D-4e0e-8A58-DC2708C1BA59", "Snippet", 
        ImagePath = "Images.page_code.png", 
        Description = "Insert snippets of text, possibly incorporating the currently selected text or the contents of the clipboard",
        HasEditableOptions = true, 
        PublisherUrl = "http://infiniteimprobability.blogspot.com/")]
    [InsertableContentSource("Snippet")]
    public class Plugin : ContentSource {
        Data.Config config;

        public Plugin() {
            config = Data.Config.Load();
        }

        public override DialogResult CreateContent(IWin32Window dialogOwner, ref string content) {
            Data.Snippet snippet = new SnippetPlugin.Data.Snippet();
            Forms.Select selectForm = new SnippetPlugin.Forms.Select(config, snippet);
            Point formLocation = Cursor.Position;
            formLocation.Offset(-(selectForm.Size.Width - 30), -30);
            selectForm.Location = formLocation;
            if(selectForm.ShowDialog() == DialogResult.OK) {
                // TODO: this is here to support data entry fields when I add them
                Dictionary<string, string> variableValues = new Dictionary<string, string>();
                string clipboard = Clipboard.GetText();
                variableValues.Add("content", content);
                variableValues.Add("clipboard", clipboard);
                variableValues.Add("contentorclipboard", content != "" ? content : clipboard);
                variableValues.Add("clipboardorcontent", clipboard != "" ? clipboard : content);

                content = Content.ReplaceVariables(snippet.Content, variableValues);
                return DialogResult.OK;
            } else {
                return DialogResult.Cancel;
            }
        }

        public override void EditOptions(IWin32Window dialogOwner) {
            Forms.Options optionsForm = new SnippetPlugin.Forms.Options(config);
            if(optionsForm.ShowDialog(dialogOwner) == DialogResult.OK) {
                config.Save();
            }
        }
    }
}
